package socketTest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author zyc
 * @create 2022--05--06 10:23
 */
public class MyClient {

    public Object send(String message, String host, int port) {
        Socket socket = null;
        try {
            socket = new Socket(host, port);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            Object acc = objectInputStream.readObject();
            return acc;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        MyClient myClient = new MyClient();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            Object recive = myClient.send(scanner.next(), "127.0.0.1", 5555);
            System.out.println("客户端接收到的信息为:" + recive);
        }
    }
}
